﻿using System;
using System.Web.Mvc;

using Ninject.Core;
using Ninject.Conditions;
using Ninject.Conditions.Builders;
using Ninject.Core.Activation;
using Ninject.Core.Parameters;

using BlueSky.Web.Common;

namespace BlueSky.Web.Extentions
{
    public static class ContextConditionBuilderExt
    {
        public static ICondition<IContext> AsController<TController>(this ContextConditionBuilder<IContext, IContext> context)
            where TController : Controller
        {
            return context.AsController(Utils.GetControllerName<TController>());
        }

        public static ICondition<IContext> AsController(this ContextConditionBuilder<IContext, IContext> context, Type controllerType)
        {
            return context.AsController(Utils.GetControllerName(controllerType));
        }

        public static ICondition<IContext> AsController(this ContextConditionBuilder<IContext, IContext> context, string controllerName)
        {
            return context.Variable("controllerName").EqualTo(controllerName);
        }





        public static ParameterCollectionBuilder ByController<TController>(this ParameterCollectionBuilder param)
            where TController : Controller
        {
            return param.ByController(Utils.GetControllerName<TController>());
        }

        public static ParameterCollectionBuilder ByController(this ParameterCollectionBuilder param, Type controllerType)
        {
            return param.ByController(Utils.GetControllerName(controllerType));
        }

        public static ParameterCollectionBuilder ByController(this ParameterCollectionBuilder param, string controllerName)
        {
            return param.ContextVariable("controllerName", controllerName);
        }



        public static ICondition<IContext> AsTarget(this ContextConditionBuilder<IContext, IContext> context, string target)
        {
            return context.Target.Tag.EqualTo(target);
        }
    }
}
